//
//  YWRemoveObserverTableViewController.m
//  maintain
//
//  Created by kyo on 16/11/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWRemoveObserverTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "UIScrollView+EmptyDataSet.h"

#import "YWHandler.h"
#import "YWSelectContanctCell.h"

@interface YWRemoveObserverTableViewController ()<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>{
    NSIndexPath *lastIndexPath;
}
//选择的人的数组
@property (nonatomic, strong) NSMutableArray *selectedContactArray;
@end

@implementation YWRemoveObserverTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"removeObserver", nil);//@"删除参与人";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    
    [self initTableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"cancel", nil) style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemEvent:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"delete", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];
    if (self.dataSource.count == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.rowHeight = 60.0f;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    [self.tableView setEditing:YES];
}

- (NSMutableArray *)selectedContactArray{
    if (!_selectedContactArray) {
        _selectedContactArray = [NSMutableArray array];
    }
    return _selectedContactArray;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWSelectContanctCell *cell = [YWSelectContanctCell cellWithTableView:tableView];
    YWHandler *contact = _dataSource[indexPath.row];
    cell.contact = contact;
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YWHandler *contact = _dataSource[indexPath.row];
    if ([self.selectedContactArray containsObject:contact]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWHandler *contact = _dataSource[indexPath.row];
    //单选
    if (![self.selectedContactArray containsObject:contact]) {
        [self.selectedContactArray removeAllObjects];
        [self.selectedContactArray addObject:contact];
    }
    if (lastIndexPath) {
        [tableView deselectRowAtIndexPath:lastIndexPath animated:NO];
    }
    lastIndexPath = indexPath;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWHandler *contact = _dataSource[indexPath.row];
    if ([self.selectedContactArray containsObject:contact]) {
        [self.selectedContactArray removeObject:contact];
    }
    lastIndexPath = nil;
}

#pragma mark - private method
- (void)leftBarButtonItemEvent:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)rightBarButtonItemEvent:(id)sender{
    //单选
    if (_selectedContactArray.count==1) {
        YWHandler *handler = _selectedContactArray[0];
        NSString *tip = [NSString stringWithFormat:@"%@%@%@%@%@",NSLocalizedString(@"youWill", nil),handler.userName?:@"",NSLocalizedString(@"removeObserverAndAfterRemove", nil),handler.userName?:@"",NSLocalizedString(@"willNotFollowMissionProgress", nil)];
        UIAlertController* alertController= [UIAlertController alertControllerWithTitle:tip message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([self.delegate respondsToSelector:@selector(deleteUser:)]) {
                [self dismissViewControllerAnimated:YES completion:^{
                    [self.delegate deleteUser:handler];
                }];
            }
        }]];
        
    }else{
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectObserverWhichWillBeRemove", nil)];
    }
    
}


@end
